package com.chengh.vhr.controller.system;

import com.chengh.vhr.model.Hr;
import com.chengh.vhr.model.HrRole;
import com.chengh.vhr.model.RespBean;
import com.chengh.vhr.model.Role;
import com.chengh.vhr.service.HrRoleService;
import com.chengh.vhr.service.HrService;
import com.chengh.vhr.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/hr")
public class HrController {

    @Autowired
    HrService hrService;

    @Autowired
    RoleService roleService;

    @Autowired
    HrRoleService hrRoleService;

    /**
     * 查询所有的用户,就是hr
     * @author chengh
     * @return 用户列表
     */
    @GetMapping("/")
    public List<Hr> getAllHrs(@RequestParam String keywords) {

        return hrService.getAllHrs(keywords);
    }

    /**
     * 更新用户状态
     * @param hr 用户信息
     * @Author 程航
     * @return
     */
    @PutMapping("/")
    public RespBean updateHr(@RequestBody Hr hr) {
        System.out.println(hr);
        if(hrService.updateHr(hr) == 1) {
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }

    /**
     * 根据id删除所有hr
     * @param id hrid
     * @Author chengh
     * @return
     */
    @DeleteMapping("/{id}")
    public RespBean deleteHr(@PathVariable  int id) {
        if(hrService.deleteHr(id) == 1) {
            return RespBean.ok("删除成功!");
        }
        return RespBean.error("删除失败!");
    }

    /**
     * 获得所有的角色
     * @Author chengh
     * @return 角色列表
     */
    @GetMapping("/roles")
    public List<Role> allRoles() {
        return roleService.getAllRole();
    }

    @PutMapping("/updateHrRole")
    public RespBean updateHrRole(@RequestBody Map<String, Object> map) {
        int hrId = 0;
        List<Integer>  roleIds = new ArrayList<>();
        if(map.containsKey("hrId")) {
            hrId = (Integer) map.get("hrId");
            if(map.containsKey("roleIds")) {
                roleIds = (List<Integer>) map.get("roleIds");
            }
        }
        roleIds.forEach(item-> {
            System.out.println(item);
        });
        if(hrId != 0) {
            if(hrRoleService.updateHrRole(hrId,roleIds )) {
                return RespBean.ok("更新角色成功!");
            }
            return RespBean.error("更新角色失败!");
        }
        return RespBean.error("传入的hrId为空,请联系管理员");
    }
}
